package de.fhdw.hbs.client.gui;

import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;


/*
 * http://layerxml.wordpress.com/2010/09/29/auto-collapsing-jxtaskpane/ as
 * template
 */
/**
 * 
 * @author SZAHEL
 *
 */
class CollapseListener implements PropertyChangeListener {
	private final JXTaskPaneContainer parent;
	private final JXTaskPane pane;

	public CollapseListener(JXTaskPaneContainer parent,
							JXTaskPane pane) {
		this.parent = parent;
		this.pane = pane;
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		//Event if pane is expanded
		if (evt.getPropertyName().equals("collapsed") && evt.getNewValue().toString().equals("false")) {
			collapseAllPanesExceptChoosen(pane);
			increaseChoosenPaneSize(pane);
		}
		//Event if pane is collapsed
		if (evt.getPropertyName().equals("collapsed") && evt.getNewValue().toString().equals("true")) {
			decreaseChoosenPaneSize(pane);
		}
	}
	private void collapseAllPanesExceptChoosen( JXTaskPane choosenPane) {

		for (int i = 0; i < parent.getComponents().length; i++) {
			if (parent.getComponents()[i] instanceof JXTaskPane) {
				JXTaskPane pane = (JXTaskPane) parent.getComponents()[i];
				if (! pane.equals(choosenPane) & !pane.isCollapsed()) {
					pane.setCollapsed(true);
				}
			}
		}
	}
	private void increaseChoosenPaneSize( JXTaskPane choosenPane) {

		for (int i = 0; i < parent.getComponents().length; i++) {
			if (parent.getComponents()[i] instanceof JXTaskPane) {
				JXTaskPane pane = (JXTaskPane) parent.getComponents()[i];
				if (pane.equals(choosenPane) ) {
					pane.setPreferredSize(new Dimension(parent.getWidth(),parent.getHeight()));
					pane.setSpecial(true);
				}
			}
		}
	}
	private void decreaseChoosenPaneSize( JXTaskPane choosenPane) {

		for (int i = 0; i < parent.getComponents().length; i++) {
			if (parent.getComponents()[i] instanceof JXTaskPane) {
				JXTaskPane pane = (JXTaskPane) parent.getComponents()[i];
				if (pane.equals(choosenPane) ) {
					pane.setPreferredSize(new Dimension(parent.getWidth(),parent.getHeight()/10));
					pane.setSpecial(false);
				}
			}
		}
	}
}


